package com.jinmdz.fmis.api.admin.controller;

import com.jinmdz.fmis.api.admin.model.organization.OrganizationIdData;
import com.jinmdz.fmis.api.admin.model.organization.OrganizationLoadData;
import com.jinmdz.fmis.api.admin.model.organization.OrganizationSaveData;
import com.jinmdz.fmis.api.admin.model.organization.OrganizationTreeItem;
import com.jinmdz.fmis.api.admin.service.OrganizationService;
import com.jinmdz.fmis.api.api.model.common.IdData;
import com.jinmdz.fmis.api.api.model.common.IdVersionData;
import com.jinmdz.fmis.api.base.BaseController;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.dao.admin.model.organization.OrganizationListItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 组织机构维护信息相关接口
 *
 * @author LiCongLu
 * @date 2020-03-16 11:37
 */
@Api(description = "组织机构维护信息相关接口")
@RestController()
@RequestMapping("/admin/organization")
public class OrganizationController extends BaseController {

    @Resource
    private OrganizationService organizationService;


    /**
     * 组织机构树，根据组织机构父节点parent_id加载表【organization_institution】中保存的子机构数据
     *
     * @param data 父主键信息
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:37
     */
    @ApiOperation(value = "组织机构树，根据组织机构父节点parent_id加载表【organization_institution】中保存的子机构数据", notes = "组织机构树，根据组织机构父节点parent_id加载表【organization_institution】中保存的子机构数据")
    @PostMapping("/load-organization-tree-with-parent-id")
    public BaseResult<ArrayList<OrganizationTreeItem>> loadOrganizationTreeWithParentId(@RequestBody IdData data) {
        return resultHandler(userItem -> organizationService.loadOrganizationTreeWithParentId(data));
    }

    /**
     * 根据组织机构父节点parent_id加载表【organization_institution】中保存的子组织机构数据
     *
     * @param data 父主键信息
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:37
     */
    @ApiOperation(value = "根据组织机构父节点parent_id加载表【organization_institution】中保存的子组织机构数据", notes = "根据表【organization_institution】中父组织机构parent_id加载子组织机构数据")
    @PostMapping("/load-organization-list-with-parent-id")
    public BaseResult<ArrayList<OrganizationListItem>> loadOrganizationListWithParentId(@RequestBody IdData data) {
        return resultHandler(userItem -> organizationService.loadOrganizationListWithParentId(data));
    }

    /**
     * 根据组织机构id加载表【organization_institution】中保存的组织机构数据
     *
     * @param data 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:37
     */
    @ApiOperation(value = "根据组织机构id加载表【organization_institution】中保存的组织机构数据", notes = "根据表【organization_institution】中id加载组织机构数据")
    @PostMapping("/load-organization-with-id")
    public BaseResult<OrganizationLoadData> loadOrganizationWithId(@RequestBody OrganizationIdData data) {
        return resultHandler(data, userItem -> organizationService.loadOrganizationWithId(data));
    }

    /**
     * 保存组织机构信息到organization_institution表中
     *
     * @param data 组织机构请求数据
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:37
     */
    @ApiOperation(value = "保存组织机构信息到【organization_institution】表中", notes = "保存组织机构信息到【organization_institution】表中")
    @PostMapping("/save-organization")
    public BaseResult saveOrganization(@RequestBody OrganizationSaveData data) {
        return resultHandler(data, userItem -> organizationService.saveOrganization(userItem, data));
    }

    /**
     * 从organization_institution表中删除指定id的组织机构信息记录
     *
     * @param data 主键和版本号
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:37
     */
    @ApiOperation(value = "从organization_institution表中删除指定id的组织机构信息记录", notes = "从organization_institution表中删除指定id的组织机构信息")
    @PostMapping("/delete-organization-with-id")
    public BaseResult deleteOrganizationWithId(@RequestBody ArrayList<IdVersionData> data) {
        return resultHandler(data, userItem -> organizationService.deleteOrganizationWithId(userItem, data));
    }
}
